package cn.zzzhy.util;

import cn.zzzhy.entity.URL;
import cn.zzzhy.spider.DownloadPool;
import cn.zzzhy.spider.SpiderManager;
import cn.zzzhy.spider.SpiderPool;
import com.google.common.base.Strings;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zzzhy
 * @since 16/2/27 下午4:08
 */
public class URLParser {

    private final static Pattern PARSE_URL = Pattern.compile("(http[s]?)?(://)?([\\w,.]+):?([0-9]*)?([/.,\\w]+)?");
    private final static String IMAGE_LINK = "([\\w,.:/]+)((.png)|(.jpeg)|(.jpg)|(.gif))";
    private final static String MV_LINK = "([\\w,.:/]+)((.mp4)|(.avi)|(.mkv)|(.rmvb))";
    private final static String ALL_LINK = "([\\w,.:/]+)((.png)|(.jpeg)|(.gif)|(.jpg)|(.mp4)|(.avi)|(.mkv)|(.rmvb))";
    private final static String HTML_URL = "(http[s]?)?(://)+([\\w,.:/])+";

    public static void parse(String pageContent) {
//        System.out.println(pageContent);
        SpiderManager.FetchType fetchType = SpiderManager.fetchType == null ? SpiderManager.FetchType.All : SpiderManager.fetchType;
        try {
            String regex,tempUrl;
            switch (fetchType){
                case IMAGE:
                    regex = IMAGE_LINK;
                    break;
                case MV:
                    regex = MV_LINK;
                    break;
                default:
                case All:
                    regex = ALL_LINK;
                    break;
            }
            Matcher m = Pattern.compile(regex).matcher(pageContent);
            while (m.find()) {
                tempUrl = m.group();
                if(!tempUrl.startsWith("http")){
                    while(tempUrl.startsWith("/")){
                        tempUrl = tempUrl.substring(1);
                    }
                    tempUrl = "http://".concat(tempUrl);
                }
                synchronized (DownloadPool.download){
                    if(DownloadPool.download.add(tempUrl)){
                        SpiderManager.client.getOutText().append("抓到可下载路径一枚,url:"+tempUrl+"\r\n");
//                    DownloadPool.download.notifyAll();
                    }
                    DownloadPool.download.notifyAll();
                }
            }
            m = Pattern.compile(HTML_URL).matcher(pageContent);
            while (m.find()){
                tempUrl = m.group();
                if(!tempUrl.startsWith("http://")){
                    while(tempUrl.startsWith("/")){
                        tempUrl = tempUrl.substring(1);
                    }
                    tempUrl = "http://"+tempUrl;
                }
                if(SpiderPool.all.add(tempUrl)){
                    SpiderManager.client.getOutText().append("抓到野生路径一枚,url:"+tempUrl+"\r\n");
                    SpiderPool.all.add(tempUrl);
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }


    public static URL parseUrl(String destUrl) {
        Matcher m = PARSE_URL.matcher(destUrl);
        if(m.find()){
            String host = m.group(3);
            String port = m.group(4);
            String location = m.group(0);
            return new URL(host, Strings.isNullOrEmpty(port) ? 80 : Integer.valueOf(port),location);
        }
        return null;
    }

    public static void main(String[] args) {
        Pattern IDENTIFY_INFO_PATTERN = Pattern.compile("(http[s]?)?(://)?([\\w,.]+):?([0-9]*)?([/,.,\\w]+)?");
        Matcher m = Pattern.compile(IMAGE_LINK).matcher("http://mycd.qq.com/tfs/forum/201605/17/211252e118vvvw4mowvodd.jpg");
        if(m.find()){
            System.out.println(m.group(5));
            System.out.println(m.group(4)); //8080
            System.out.println(m.group(3)); //www.baidu.com
            System.out.println(m.group(2));
            System.out.println(m.group(1));
            System.out.println(m.group(0)); //http://www.baidu.com:8080/test.html
        }
    }
}
